package SA.teacher;

import java.io.EOFException;
import java.sql.*;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.util.Log;

public class SAteacherActivity extends Activity {
	
	Data database = null;
	public String serveridpw1;
	public String teacherID;
	public String teacherPW;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		ImageView loginBtn = (ImageView) findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				database = new Data();
				
				EditText eTextID = (EditText) findViewById(R.id.eTextID);
				EditText eTextPW = (EditText) findViewById(R.id.eTextPW);

				teacherID = eTextID.getText().toString();
				teacherPW = eTextPW.getText().toString();
				String clientidpw = teacherID + teacherPW;
				Log.e("응답 : ", "0");
				//db접속
				//database.update(studentID);
				Log.e("응답 : ", "1");
				
				login(clientidpw);
			}
		});
	}

	void login(String clientidpw) {
// 맞다면 로그인, 틀리면 창띄우기
		Log.e("login 응답 : ", "1");
		Parse ps = new Parse();
		Log.e("login 응답 : ", "2");
		ps.run();
		Log.e("login 응답 : ", "3");
		Log.e("응답 : ", serveridpw1);
		Log.e("login 응답 : ", "4");
		 if (clientidpw.equals(serveridpw1)) {

		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);

		
		 } else { new
		  AlertDialog.Builder(SAteacherActivity.this).setTitle("로그인 실패")
		  .setMessage("아이디 비번 확인").setCancelable(false) .setNegativeButton("다시 로그인",
		  null).show(); }
		 
	}
	
	
	private class Parse implements Runnable {
		public void run() {
			// TODO Auto-generated method stub
			try {
				String addr ="Http://220.67.128.8/search.php?teacherID="+teacherID+"&teacherPW="+teacherPW;
				
				URL url = new URL(addr);
				
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				
				InputStream  is   = null;
				ByteArrayOutputStream baos = null; 
				Log.e("login 응답 : ", "접속0");
				if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
					Log.e("login 응답 : ", "접속1");
					is = conn.getInputStream();  
					Log.e("login 응답 : ", "접속2");
					baos = new ByteArrayOutputStream();
					Log.e("login 응답 : ", "접속3");
					byte[] byteBuffer = new byte[1024];    byte[] byteData = null; 
					int nLength = 0;
					Log.e("login 응답 : ", "접속4");
					while((nLength = is.read(byteBuffer)) > 0) {   
						baos.write(byteBuffer, 0, nLength); 
					}
					Log.e("login 응답 : ", "접속5");
					byteData = baos.toByteArray(); 
					Log.e("login 응답 : ", "접속6");
					//String response = new String(byteData, "euc-kr");   
					String response = new String(byteData);  
					Log.e("login 응답 : ", "접속7");
					Log.e("login 응답 : ", response);
					serveridpw1 = response;
					Log.e("login 응답 : ", serveridpw1);
					if(response==null || response.equals("")) {    
						throw new Exception("Response is NULL!");  
					}
				}
				else{
					Log.e("login 응답 : ", "접속 안됨");
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}
	
	
	
	
	public class Data {
		private Connection con = null;
		private Statement stmt = null;
		String url = "jdbc:mysql://220.67.128.8/APM_Setup/Server/MySQL5/data/asaacdemy?useUnicode=true&characterEncoding=euckr";
		String driver = "com.mysql.jdbc.Driver";
		String user = "root";
		String pwd = "sa";

		Data() {
			try {
				Log.e("db응답 : ", "2-0");
				Class.forName(driver);
				Log.e("db응답 : ", "2-1");
				con = DriverManager.getConnection(url, user, pwd);
				Log.e("db응답 : ", "2-2");
				stmt = con.createStatement();
				//stmt.executeUpdate("set names euckr");
				
			} catch (Exception ee) {
				System.err.println("DB연결 오류");
			}
		}

		public void update(String string) {Log.e("db응답 : ", "0");
			try {Log.e("db응답 : ", "1");
				//toMySQL(string);
				String sql = "select * from student_info where ID=" + string
						+ ";" ;
				Log.e("db응답 : ", "2");
				stmt = con.createStatement();
				Log.e("db응답 : ", "3");
				stmt.executeUpdate(sql);
				
				System.out.println("저장 완료");
			} catch (SQLException e) {
				e.printStackTrace();
				}	}

		
		public String toMySQL(String str){
			try{
				if(str != null)
					return new String(str.getBytes("MS949") , "8859_1");
				else
					return null;
			}catch(Exception e){
				return null;
			}
		}
	}
	
}